<!--
 * @Description: 分配岗位
 * 
 * @Author: XiongYaoYuan<xiongyaoyuan>
 * @Date: 2023-06-13 20:35:29
 * @FilePath: /yuxiuproweb/src/views/system/user/tree/index.vue
-->
<template>
  <el-dialog
    :close-on-click-modal="false"
    title="分配岗位"
    :visible.sync="shows"
    width="500px"
    append-to-body
  >
    <el-tree
      :data="processList"
      show-checkbox
      ref="tree"
      node-key="processId"
      :default-checked-keys="checkedKeys"
      default-expand-all
      :props="defaultProps"
    >
      <!-- :default-expanded-keys="[1, 2, 3]"
      :default-checked-keys="[5, 8, 10]" -->
    </el-tree>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="shows = false">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { addOrUpdate, selectListByUserId } from "@/api/system/user";
import { listProcess } from "@/api/process/production";
import { delDepartTree } from "@/views/process/production/helper.js";

export default {
  props: {
    show: Boolean,
    data: {
      type: Object,
    },
  },
  data() {
    return {
      defaultProps: {
        children: "children",
        label: "name",
      },
      processList: [],
      selectList: [],
      checkedKeys: [],
    };
  },
  computed: {
    shows: {
      get() {
        return this.show;
      },
      set(newValue) {
        this.$emit("update:show", newValue);
      },
    },
  },
  mounted() {
    this.getList();
    this.getSelectListByUserId();
  },
  methods: {
    /** 查询工序模型列表 */
    getList() {
      this.loading = true;
      listProcess(this.queryParams).then(({ rows }) => {
        this.processList = delDepartTree(
          rows.map((e) => ({
            ...e,
            processId: `${e.processId}`,
          }))
        );
        console.log(this.processList);
      });
    },
    getSelectListByUserId() {
      selectListByUserId({
        userId: this.data.userId,
      }).then(({ data }) => {
        this.selectList = data || [];
        this.checkedKeys = this.selectList.map((e) => e.processId);
      });
    },
    submitForm() {
      const list = this.$refs.tree.getCheckedNodes();
      const sysUserProcessList = list?.map((e) => {
        return {
          processId: e.processId,
          processName: e.name,
          userId: this.data.userId,
          username: this.data.nickName,
          sysUserProcessId:
            this.selectList.find((v) => v.processId === e.processId)
              ?.sysUserProcessId || null,
        };
      });
      addOrUpdate(sysUserProcessList).then((res) => {
        this.$modal.msgSuccess("保存成功");
        this.shows = false;
      });
    },
  },
};
</script>
